package br.com.tcc.screens;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;

import br.com.tcc.core.Controller;
import br.com.tcc.core.Mouse;
import br.com.tcc.helper.HighScore;
import br.com.tcc.helper.Player;

public class TelaPontuacao extends Canvas implements Runnable {

	private static final long serialVersionUID = 1L;
	Thread thread;
	private Image imgLogo;
	private Image botaoVoltar;
	private BufferedImage bufferImage;
	private Graphics2D mGraphics;
	private boolean isRunning;
	List<HighScore> scores;
	Mouse mouse;
	Player player;
	
	boolean isMouseOn = false;
	boolean novoHighScore = false;
	Controller controller;
	
	
	public TelaPontuacao(Controller control, List<HighScore> listaScore, Player player) {
		this.player = player;
		this.controller = control;
		isRunning = true;
		this.setIgnoreRepaint(true);
		this.setBounds(0, 0, 640, 400);
		this.setBackground(Color.black);
		scores = listaScore;
		mouse = new Mouse();
		this.addMouseListener(mouse);
		this.addMouseMotionListener(mouse);
		requestFocus();
		
		try {
			imgLogo = ImageIO.read(new File("imagens/scoreScreen.jpg"));
			botaoVoltar = ImageIO.read(new File("imagens/voltar.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		defineListaScore();
		startAnimation();
	}

	private void defineListaScore() {
		System.out.println("NOME: "+player.getNome());
		System.out.println("PONTOS: " + player.getPontos());
		System.out.println("CARGO: " + player.getCargoEscolhido());

		if (scores.size() > 0) {
			List<HighScore> scoresList = scores;
			player.setPontos(player.getPontos()); // 40
			HighScore highScore = new HighScore();
			highScore.setNome(player.getNome());
			highScore.setPontos(player.getPontos());
			scoresList.add(highScore);
			
			
			Collections.sort(scoresList);
			scoresList.remove(scoresList.size() - 1);
			Collections.sort(scoresList);

			
			//System.out.println("PLAYER NAME: "+player.getNome());
			//System.out.println("PLAYER POINT: "+player.getPontos());
			
			for (int i = 0; i < scoresList.size(); i++) {
				//System.out.println("SCORE NAME: "+scoresList.get(i).getNome());
				//System.out.println("SCORE POINT: "+scoresList.get(i).getPontos());
				if (scoresList.get(i).getNome() == player.getNome()
						&& scoresList.get(i).getPontos() == player.getPontos()) {
					novoHighScore = true;
					System.out.println("ENTROU AQUI");
				}
			}

		}
	}

	public void startAnimation() {
		if (thread == null) {
			thread = new Thread(this);
			thread.start();
		}
	}

	public void stopAnimation() {
		if (thread != null) {
			thread = null;
		}
	}

	@Override
	public void run() {
		while (isRunning) {
			long startTime = System.currentTimeMillis();
			
			checkUserInput();
			checkCollision();
			render();
			
			//System.out.println("RODANDO TELA PONTUACAO");
			
			paintScreen();
			long timeSinceStart = (startTime - System.currentTimeMillis());
			if (timeSinceStart < (1000 / 25)) {
				try {
					Thread.sleep((1000 / 25) - timeSinceStart);
				} catch (java.lang.InterruptedException e) {
				}
			}
		}

	}

	public void render() {
		if (bufferImage == null) {
			// bufferImage = createImage(640, 400);
			bufferImage = new BufferedImage(640, 400,
					BufferedImage.TYPE_INT_ARGB);
			if (bufferImage == null) {
				System.out.println("dbImage is null");
				return;
			} else {
				mGraphics = (Graphics2D) bufferImage.createGraphics();
			}
		}
		
		mGraphics.setColor(Color.white);
		mGraphics.setFont(new Font("Arial", Font.BOLD, 18));
		mGraphics.drawImage(imgLogo, 0, 0, null);
		for (int i = 0; i < scores.size(); i++) {
			mGraphics.drawString(scores.get(i).getNome(), 140, 150+(i*25));
			mGraphics.drawString(String.valueOf(scores.get(i).getPontos()), 490, 150+(i*25));
		}
		
		
		mGraphics.drawImage(botaoVoltar, 260, 300, null);
		//mGraphics.drawRect(260, 300, 120, 40);
		if(novoHighScore){
			mGraphics.setColor(Color.BLUE);
			mGraphics.drawString("NOVA PONTUACAO!!!", 225, 60);
		}
	}
	
	public void checkCollision(){
		if ((mouse.getMousePosition().getX() > 260) && ( mouse.getMousePosition().getX() < 380)
				&& (mouse.getMousePosition().getY() > 300) && (mouse.getMousePosition().getY() < 340) ) {
			isMouseOn = true;
		}
	}
	
	private void checkUserInput() {
		if (isMouseOn && mouse.isMousePressed()) {
			isRunning = false;
			stopAnimation();
			controller.mostraForm(this);
		}
		isMouseOn = false;
		mouse.setMousePressed(false);
	}
	
	private void paintScreen() throws NullPointerException
	// actively render the buffer image to the screen
	{
		Graphics2D g;
		try {
			g = (Graphics2D) this.getGraphics(); // get the panel's graphic
													// context
			if (g == null) {
				// System.out.println("teste 222");
			}
			if ((g != null) && (bufferImage != null))
				g.drawImage(bufferImage, 0, 0, null);
			Toolkit.getDefaultToolkit().sync(); // sync the display on some
												// systems
			g.dispose();
		} catch (Exception e) {
			// System.out.println("Graphics context error: " + e);
		}
	}

}
